<template>
  <div>
    <!-- 工厂信息 -->
    <basic-container shadow="hover">
      <curd-title required :title="'工厂信息'" />
      <avue-crud
        :table-loading="factoryloading"
        :option="factoryOption"
        :data="factoryList"
        @on-load="onFactoryLoad"
        @row-save="rowFactorySave"
        @row-update="rowFactoryUpdate"
        @refresh-change="refreshFactoryChange"
        @row-del="rowFactoryDel"
      >
      </avue-crud>
    </basic-container>
    <!-- 产品信息 -->
    <basic-container shadow="hover">
      <curd-title required :title="'产品信息'" />
      <avue-crud
        :table-loading="productloading"
        :option="productOption"
        :data="productList"
        @on-load="onProductLoad"
        @row-save="rowSaveProduct"
        @row-update="rowProductUpdate"
        @refresh-change="refreshProductChange"
        @row-del="rowProductDel"
      >
      </avue-crud>
    </basic-container>
    <!-- 客户/市场信息 -->
    <basic-container shadow="hover">
      <curd-title :title="'客户/市场信息'" />
      <avue-crud
        :table-loading="customerLoading"
        :option="customerOption"
        :data="customerList"
        @on-load="onCustomerLoad"
        @row-save="rowCustomerSave"
        @row-update="rowCustomerUpdate"
        @refresh-change="refreshCustomerChange"
        @row-del="rowCustomerDel"
      >
      </avue-crud>
    </basic-container>
    <!-- 原料信息 -->
    <basic-container shadow="hover">
      <curd-title :title="'原料信息'" />
      <avue-crud
        :table-loading="materialLoading"
        :option="materialOption"
        :data="materialList"
        @on-load="onMaterialLoad"
        @row-save="rowMaterialSave"
        @row-update="rowMaterialUpdate"
        @refresh-change="refreshMaterialChange"
        @row-del="rowMaterialDel"
      >
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import CurdTitle from '@/components/base/title'
import { factoryOption, productOption, customerOption, materialOption } from '@/option/tenant/supplier/factory-info'
import { getFactoryList, removeFactory, addFactory, updateFactory } from '@/api/supplier/factory'
import { getProductList, removeProduct, addProduct, updateProduct } from '@/api/supplier/product'
import { getCustomerList, removeCustomer, addCustomer, updateCustomer } from '@/api/supplier/customer'
import { getMaterialList, removeMaterial, addMaterial, updateMaterial } from '@/api/supplier/material'
export default {
  name: 'FactoryInfo',
  components: { CurdTitle },
  inject: ['id'],
  data() {
    return {
      factoryList: [],
      factoryloading: false,
      factoryOption: factoryOption.call(this),
      // 产品信息
      productloading: false,
      productList: [],
      productOption: productOption.call(this),
      // 客户/市场信息
      customerLoading: false,
      customerList: [],
      customerOption: customerOption.call(this),
      // 原料信息
      materialLoading: false,
      materialList: [],
      materialOption: materialOption.call(this)
    }
  },
  methods: {
    onFactoryLoad() {
      this.factoryloading = true
      getFactoryList({ supplierId: this.id }).then(res => {
        this.factoryList = res.data.data.records
        this.factoryloading = false
      })
    },
    refreshFactoryChange() {
      this.onFactoryLoad()
    },
    rowFactorySave(row, done, loading) {
      addFactory({ ...row, supplierId: this.id }).then(
        () => {
          this.onFactoryLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowFactoryUpdate(row, index, done, loading) {
      updateFactory(row).then(
        () => {
          this.onFactoryLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowFactoryDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeFactory(row.id)
        })
        .then(() => {
          this.onFactoryLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    },
    // 产品信息
    onProductLoad() {
      this.productloading = true
      getProductList({ supplierId: this.id }).then(res => {
        this.productList = res.data.data.records
        this.productloading = false
      })
    },
    refreshProductChange() {
      this.onProductLoad()
    },
    rowSaveProduct(row, done, loading) {
      addProduct({ ...row, supplierId: this.id }).then(
        () => {
          this.onProductLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowProductUpdate(row, index, done, loading) {
      updateProduct(row).then(
        () => {
          this.onProductLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowProductDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeProduct(row.id)
        })
        .then(() => {
          this.onProductLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    },
    // 客户/市场信息
    onCustomerLoad() {
      this.customerLoading = true
      getCustomerList({ supplierId: this.id }).then(res => {
        console.log(res.data.data.records)
        this.customerList = res.data.data.records
        this.customerLoading = false
      })
    },
    refreshCustomerChange() {
      this.onCustomerLoad()
    },
    rowCustomerSave(row, done, loading) {
      addCustomer({ ...row, supplierId: this.id }).then(
        () => {
          this.onCustomerLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowCustomerUpdate(row, index, done, loading) {
      updateCustomer(row).then(
        () => {
          this.onCustomerLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowCustomerDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeCustomer(row.id)
        })
        .then(() => {
          this.onCustomerLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    },
    // 原料信息
    onMaterialLoad() {
      this.materialLoading = true
      getMaterialList({ supplierId: this.id }).then(res => {
        this.materialList = res.data.data.records
        this.materialLoading = false
      })
    },
    refreshMaterialChange() {
      this.onMaterialLoad()
    },
    rowMaterialSave(row, done, loading) {
      addMaterial({ ...row, supplierId: this.id }).then(
        () => {
          this.onMaterialLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowMaterialUpdate(row, index, done, loading) {
      updateMaterial(row).then(
        () => {
          this.onMaterialLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowMaterialDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeMaterial(row.id)
        })
        .then(() => {
          this.onMaterialLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    }
  }
}
</script>

<style></style>
